package com.xiaoyu.client.ui.activity.nearby;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.xiaoyu.client.R;
import com.xiaoyu.client.adapter.nearby.ShopTagAdapter;
import com.xiaoyu.client.model.nearby.ShopTagBean;
import com.xiaoyu.client.model.nearby.ShopTagParam;
import com.xiaoyu.client.network.NetworkManager;
import com.xiaoyu.commonlib.network.CommonHttpResponser;
import com.xiaoyu.commonlib.ui.base.BaseActivity;
import com.xiaoyu.commonlib.utils.ToastUtil;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ShopTypeActivity extends BaseActivity {
    @BindView(R.id.activity_shop_type_list_container)
    ListView mListContainer;

    private List<ShopTagParam> mList = new ArrayList<>();
    private ShopTagAdapter mAdapter;
    private Map<String,String> mSelectList = new HashMap<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shop_type);
        ButterKnife.bind(this);
        mSelectList = (Map<String, String>) getIntent().getExtras().getSerializable(ShopRecomAddActivity.SHOP_TAG_LIST);
        initView();
    }

    private void initView() {
        mAdapter = new ShopTagAdapter(this,mList);
        mListContainer.setAdapter(mAdapter);
        mAdapter.setSelectList(mSelectList);
        mListContainer.setOnItemClickListener(onItemClickListener);
    }


    @Override
    protected void onResume() {
        super.onResume();
        mList.clear();
        getTypeInfo();
    }

    @OnClick(R.id.activity_shop_type_confirm)
    public void confirmClick(){
        if(mSelectList.size()==0){
            ToastUtil.showToast(this,"请选择商铺类型");
            return;
        }
        Intent intent = new Intent();
        Bundle bundle = new Bundle();
        bundle.putSerializable(ShopRecomAddActivity.SHOP_TAG_LIST, (Serializable) mSelectList);
        intent.putExtras(bundle);
        setResult(0,intent);
        finish();
    }


    //返回按钮
    @OnClick(R.id.activity_shop_type_title_back_btn)
    public void backClick(){
        finish();
    }

    @Override
    public void onBackPressed() {
        finish();
    }

    /**
     * 类型列表item监听
     *
     *  1、如果点击的item 已经存在选中列表中
     */
    private AdapterView.OnItemClickListener onItemClickListener = new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            CheckBox checkBox = view.findViewById(R.id.item_shop_type_choose_check);
            if (mSelectList.containsKey(mList.get(position).getTagId())){
                checkBox.setChecked(false);
                mSelectList.remove(mList.get(position).getTagId());
            }else {
                if (mSelectList.size() == 3){
                    ToastUtil.showToast(ShopTypeActivity.this,"最多选三项");
                }else {
                    checkBox.setChecked(true);
                    mSelectList.put(mList.get(position).getTagId(),mList.get(position).getTagName());
                }

            }
        }
    };


    /**
     * 获取商铺类型
     */
    private void getTypeInfo() {
        NetworkManager.getShopTypeForAddList(new CommonHttpResponser() {
            @Override
            public void onSuccess(String result) {
                parseTypeInfoSuc(result);
            }

            @Override
            public void onFailed(String errorCode, String errorDiscription) {

            }
        });
    }

    private void parseTypeInfoSuc(String result) {
        ToastUtil.logResult("商铺类型", result);
        ShopTagBean bean = new Gson().fromJson(result, ShopTagBean.class);
        mList.addAll(bean.getData());
        mAdapter.updateList(mList);
    }
}
